'use strict';

exports.__esModule = true;
exports.get = exports.post = undefined;

var _md = require('./md5.js');

var md5Key = '889ebb98e2667df399ce17bc01037aac';

var post = exports.post = function post(url) {
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  var now = new Date().getTime();
  var sign = md5(now, body);
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: body,
      method: 'POST',
      dataType: 'json',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        // 'Sign': sign,
        'Time': now
      },
      success: function success(res) {
        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: function fail(res) {
        reject(res);
      }
    });
  });
};

var get = exports.get = function get(url) {
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: body,
      method: 'GET',
      success: function success(res) {
        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: function fail(res) {
        reject(res);
      }
    });
  });
};

function md5(now, body) {
  var obj = Object.assign({}, body, {
    key: md5Key,
    time: now
  });
  var array = [];
  var keyArray = [];
  for (var key in obj) {
    keyArray.push(key);
  }
  keyArray.sort();
  for (var _iterator = keyArray, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
    var _ref;

    if (_isArray) {
      if (_i >= _iterator.length) break;
      _ref = _iterator[_i++];
    } else {
      _i = _iterator.next();
      if (_i.done) break;
      _ref = _i.value;
    }

    var _key = _ref;

    array.push(obj[_key]);
  }
  return (0, _md.hexMD5)(array.join(''));
}